package com.oda.camera.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.oda.resultats.util.Resultat;

import android.os.Build;
import android.os.Environment;
import android.util.Log;

public class LoadResultat {

	
	/**
	 * Tags for xml
	 */
	private static final String TAG_APPS = "oda";
	private static final String TAG_link = "colza";
	private static final String FILE="colzaAllResult.xml";
	private static final String DIR_IMAGE="ColzaAllResult";	
	/**
	 * Class for Album
	 */
	private AlbumStorageDirFactory mAlbumStorageDirFactory = null;
	
	private List<Resultat> theList=new ArrayList<Resultat>();
	private boolean isExistFile=true;
	
	/**
	 * @throws FileNotFoundException 
	 * 
	 */
	public LoadResultat(){
		super();
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
			mAlbumStorageDirFactory = new FroyoAlbumDirFactory();
		} else {
			mAlbumStorageDirFactory = new BaseAlbumDirFactory();
		}
		File file = new File(getAlbumDir().toString()+"/"+FILE);	
		FileInputStream fis;
		try {
			Log.d("the file", ""+file.getAbsolutePath());
			fis = new FileInputStream(file);
			theList=this.retreiveData(fis);
			Log.d("LOAD", "load result");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			isExistFile=false;
		}
		
		
	}
	
	
	
	/**
	 * @return the theList
	 */
	public List<Resultat> getTheList() {
		Set<Resultat> set = new HashSet<Resultat>();
        set.addAll(theList);
        ArrayList<Resultat> res = new ArrayList<Resultat>(set);
		return res;
	}



	/**
	 * @return the isExistFile
	 */
	public boolean isExistFile() {
		return isExistFile;
	}

	

	public List<Resultat> retreiveData(FileInputStream fileIn){
		ArrayList<Resultat> listRes=new ArrayList<Resultat>();
		try {
	        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();       
	        factory.setNamespaceAware(true);
	        XmlPullParser xpp = factory.newPullParser(); 
	        xpp.setInput(new InputStreamReader(fileIn));

	        xpp.nextTag();
	        xpp.require(XmlPullParser.START_TAG, null, "oda");
	        while (xpp.nextTag() == XmlPullParser.START_TAG) {
	        	//new resultat
	        	
	        	xpp.require(XmlPullParser.START_TAG, null, "resultat");
	        	Resultat res=new Resultat();
	        	
	        	xpp.nextTag();	
	        	xpp.require(XmlPullParser.START_TAG, null, "date");		            
	            res.setDate(xpp.nextText());		           
	            xpp.require(XmlPullParser.END_TAG, null, "date");
	        	
	        	xpp.nextTag();	        	 
	        	xpp.require(XmlPullParser.START_TAG, null, "biomasse");		            
	            res.setBiomasse(Float.valueOf(xpp.nextText()));		           
	            xpp.require(XmlPullParser.END_TAG, null, "biomasse");

	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "errorRate");
	            res.setErrorRate(Float.valueOf(xpp.nextText()));
	            xpp.require(XmlPullParser.END_TAG, null, "errorRate");
	            
	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "expAzote");
	            res.setExpAzote(Float.valueOf(xpp.nextText()));
	            xpp.require(XmlPullParser.END_TAG, null, "expAzote");
	            
	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "idResultat");
	            res.setIdResultat(Integer.valueOf(xpp.nextText()));
	            xpp.require(XmlPullParser.END_TAG, null, "idResultat");

	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "idUser");
	            res.setIdUser(Integer.valueOf(xpp.nextText()));
	            xpp.require(XmlPullParser.END_TAG, null, "idUser");
	            
	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "lai");
	            res.setLai(Float.valueOf(xpp.nextText()));
	            xpp.require(XmlPullParser.END_TAG, null, "lai");
	            
	            xpp.nextTag();
	            xpp.require(XmlPullParser.END_TAG, null, "resultat");
	            //results.add(new Datum(id, title));
	            listRes.add(res);
	            Log.d("VALUE", ""+res.getBiomasse()+" "+res.getErrorRate()+" "+" "+res.getLai()+" "+res.getIdResultat()+" ");
	        }
	        xpp.require(XmlPullParser.END_TAG, null, "oda");
	        return listRes;
	        
	    } catch (Throwable t) {
	       return null;
	    }
		
	}
	
	
	private File getAlbumDir() {
		File storageDir = null;

		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {

			storageDir = mAlbumStorageDirFactory
					.getAlbumStorageDir(DIR_IMAGE);
		} else {
			Log.v("",
					"External storage is not mounted READ/WRITE.");
		}
		return storageDir;
	}

}
